#!/usr/bin/env sh
set -e

die() {
  echo "$@"
  exit 1
}

fmt() {
  ag -g '.go$' | xargs -IC go fmt C
}

test() {
  go test -v ./src/*_test
}

updatemobsql() {
  GOPROXY=direct go get -v -u git.sr.ht/~mil/mobsql@master
}

um() {
  updatemobsql
}

pp() {
  USER=user
  CONN=$USER@pine64-pinephone
  rsync --delete -v -a . $CONN:/home/$USER/mobroute
  ssh -t $CONN 'sudo sh -c "
    set -e
    cd /home/'$USER'/mobroute
    ./build.sh install
  "'
}

build() {
  BINARY=mobroute
  go build -tags=sqlite_math_functions -buildvcs=false src/$BINARY.go
}

run() {
  build
  ./mobroute "$@"
}

install() {
  [ $(whoami) = "root" ] || die "Must run as root"
  build
  cp $BINARY /usr/local/bin/
  echo "Installed $BINARY to /usr/local/bin ok"

  cp scripts/* /usr/local/bin
  echo "Installed scripts to /usr/local/bin ok"
}

if [ -z "$1" ]; then
  build
else
  "$@"
fi

